package org.unit.testmgmt.ui.table;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Generic table entry.
 * 
 * @author Robert Streng
 *
 */
public class TableEntryGeneric implements ITableEntry {

	private HashMap<Integer, Object> valueMap;
	private HashMap<Integer, TableViewerColumn> columnMap;

	@Override
	public void setValue(TableViewerColumn column, Object value) {
		valueMap.put(column.getColumnIndex(), value);
	}

	@Override
	public Object getValue(TableViewerColumn column) {
		return convert(valueMap.get(column.getColumnIndex()), column);
	}

	/**
	 * Converts a object.
	 * 
	 * @param object
	 * @param column
	 * @return converted object
	 */
	private Object convert(Object object, TableViewerColumn column) {

		if (column.isCombobox()) {
			if (object instanceof ComboboxEntry) {
				return (ComboboxEntry) object;
			}
			return object;
		}

		switch (column.getType()) {
		case STRING:
			return (String) object;
		case BOOLEAN:
			return "" + object;
		case INTEGER:
			return "" + object;
		case DOUBLE:
			return (Double) object;
		case OBJECT:
			return object;
		default:
			return object;
		}
	}

	@Override
	public int compareTo(ITableEntry tblEntry, int index) {
		return 0;
	}

	@Override
	public String getLabel(int columnIndex) {
		Object result = "";

		for (Entry<Integer, TableViewerColumn> columnEntry : columnMap
				.entrySet()) {
			if (columnEntry.getKey() == columnIndex) {
				result = valueMap.get(columnIndex);
			}
		}

		if (result == null) {
			result = "";
		}
			
		
		return ""+result;
	}

	@Override
	public void setTableViewerColumns(TableViewerColumns columns) {
		valueMap = new HashMap<Integer, Object>();
		columnMap = new HashMap<Integer, TableViewerColumn>();

		for (TableViewerColumn column : columns.getColumns()) {
			valueMap.put(column.getColumnIndex(), getInitValue(column));
			columnMap.put(column.getColumnIndex(), column);
		}
	}

	/**
	 * Returns the initializer value.
	 * @param column
	 * @return initializer value
	 */
	private Object getInitValue(TableViewerColumn column) {
		if (column.isCombobox()) {
			Set<ComboboxEntry> comboboxEntrySet = column.getComboboxEntries();
			if (comboboxEntrySet != null) {
				Iterator<ComboboxEntry> comboboxEntries = comboboxEntrySet
						.iterator();
				if (comboboxEntries.hasNext())
					return comboboxEntries.next();
			}
		}

		switch (column.getType()) {
		case STRING:
			return "";
		case BOOLEAN:
			return 0;
		case INTEGER:
			return 0;
		case DOUBLE:
			return 0.0;
		case OBJECT:
			return null;
		default:
			return "";
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public TableEntryGeneric clone() {
		TableEntryGeneric tblEntry = new TableEntryGeneric();
		tblEntry.columnMap = this.columnMap;
		tblEntry.valueMap = (HashMap<Integer, Object>) this.valueMap.clone();

		return tblEntry;
	}

	@Override
	public Collection<TableViewerColumn> getTableViewerColumns() {
		return columnMap.values();
	}

	@Override
	public Collection<Object> getValues() {
		return valueMap.values();
	};

}
